import readline from 'readline'
import { Readable, Writable } from 'stream'

const readableStream = new Readable({
  objectMode: true,
  read() {}
})
const writeableStream = new Writable({
  write: function(chunk, encoding, callback) {
    console.log(`stream received input: ${chunk}`);
    callback();
  },
});

const shell = readline.createInterface({
  input: readableStream,
  output: writeableStream,
})
shell.prompt()
shell.on('line', async (input) => {
  console.log('shell.on line: ', input)
  shell.prompt()
})

process.stdin.resume()
process.stdin.on('data', (chunk) => {
  const chunktype = typeof chunk
  console.log('stdin on data chunk:', chunk, chunktype, ('string' !== chunktype) ? chunk.toString() : '')
  readableStream.push(chunk.toString());
})


// process.stdout.write('hello \n')

// import { Readable } from 'stream'

// const readableStream = new Readable({
//   objectMode: true,
//   read() {}
// })
// const writeableStream = new stream.Writable({
//   write: function(chunk, encoding, callback) {
//     console.log(`Received input: ${chunk}`);
//     callback();
//   },
// });

// process.stdin.resume()
// process.stdin.on('data', (chunk) => {
//   const chunktype = typeof chunk
//   console.log('stdin on data chunk:', chunk, chunktype, ('string' !== chunktype) ? chunk.toString() : '')
//   readableStream.push(chunk.toString());
// })

// process.stdout.write('--------- stdout.write result here ------------- \n')
// process.stdin.emit('data', 'hello')
// process.stdin.emit('data', 'world')
// process.stdin.push('hi')
